//
//  PPTimelinePictureObject.m
//  pickpic
//
//  Created by Jinxiao on 10/5/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPTimelinePictureObject.h"

@implementation PPTimelinePictureObject

+ (instancetype)pictureForParameters:(NSDictionary *)parameters {
    PPTimelinePictureObject *picture = [[PPTimelinePictureObject alloc] init];

    picture.imageID = [parameters validNumberForKey:@"pictureid"];
    picture.desc = [parameters validStringForKey:@"description"];
    picture.key = [parameters validStringForKey:@"key"];
    picture.host = [parameters validStringForKey:@"host"];
    picture.likeCount = [parameters validNumberForKey:@"like"].integerValue;
    picture.commentCount = [parameters validNumberForKey:@"comment"].integerValue;
    picture.isLiked = [parameters validNumberForKey:@"liked"].boolValue;

    return picture;
}

- (NSString *)URLString {
    if(self.key == nil || self.host == nil) {
        return nil;
    }

    return [NSString stringWithFormat:@"%@%@", self.host, self.key];
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.albumID forKey:@"albumID"];
    [encoder encodeObject:self.imageID forKey:@"imageID"];
    [encoder encodeObject:self.createDate forKey:@"createDate"];
    [encoder encodeObject:self.updateDate forKey:@"updateDate"];
    [encoder encodeObject:self.desc forKey:@"desc"];
    [encoder encodeObject:self.key forKey:@"key"];
    [encoder encodeObject:self.host forKey:@"host"];
    [encoder encodeBool:self.isLiked forKey:@"isLiked"];
    [encoder encodeInteger:self.likeCount forKey:@"likeCount"];
    [encoder encodeInteger:self.commentCount forKey:@"commentCount"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        self.albumID = [decoder decodeObjectForKey:@"albumID"];
        self.imageID = [decoder decodeObjectForKey:@"imageID"];
        self.createDate = [decoder decodeObjectForKey:@"createDate"];
        self.updateDate = [decoder decodeObjectForKey:@"updateDate"];
        self.desc = [decoder decodeObjectForKey:@"desc"];
        self.key = [decoder decodeObjectForKey:@"key"];
        self.host = [decoder decodeObjectForKey:@"host"];
        self.isLiked = [decoder decodeBoolForKey:@"isLiked"];
        self.likeCount = [decoder decodeIntegerForKey:@"likeCount"];
        self.commentCount = [decoder decodeIntegerForKey:@"commentCount"];
    }
    return self;
}

@end
